package com.mini.common;

import com.mini.vo.BusinessResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

/**
 * 全局异常处理
 *
 * @author zl
 * @version 1.0
 * @date Created in 2020/2/27
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {




    /**
     * 未登录
     *
     * @param exception
     * @return
     */
    @ExceptionHandler({UnLoginException.class})
    public ResponseEntity<Object> handleParamException(UnLoginException exception) {
        logger.error("【handleParamException】：" + exception.getMessage());
        log.error("", exception);
        BusinessResult businessResult = BusinessResult.createInstance(Long.valueOf(HttpStatus.UNAUTHORIZED.value()), exception.getMessage(), null);
        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(businessResult);
    }

    /**
     * 异常处理
     *
     * @param exception
     * @return
     */
    @ExceptionHandler({Exception.class})
    public ResponseEntity<Object> handleBusinessException(Exception exception) {
        logger.error("【handleException】：" + exception.getMessage());
        BusinessResult businessResult = BusinessResult.createInstance(Long.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()), exception.getMessage(), null);
        log.error("", exception);
        return ResponseEntity.status(HttpStatus.OK).body(businessResult);
    }

}
